<?php

/*
 * Core of OkekCMS
 * Credits: Tito, JG, LittleJ
 */

ini_set('default_charset', 'utf-8');
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
session_start();

define('START_TIME', microtime(true));

// user information
define('USER_IP', 	$_SERVER['REMOTE_ADDR']);
define('USER_AGENT', @$_SERVER['HTTP_USER_AGENT']);
define('USER_FROM', 	@$_SERVER['HTTP_REFERER']);
define('USER_LANG', 	@substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));

// internal location parameters
define('DS', 	DIRECTORY_SEPARATOR);
define('CMS', 	dirname(__FILE__) . DS);

// sub-internal location parameters
define('NEC', CMS . 'nec' . DS);
define('LANGS', NEC . 'language' . DS);
        
// REQUIRE CLASSES
$class_dir = opendir(NEC . 'classes');

while($file = readdir($class_dir))
{
    $_ex = explode('.', $file);
    $extencion = end($_ex);
    
    if($extencion != 'php')
        continue;
    
    require_once NEC . 'classes' . DS . $file;
}

$mysql = new MySQL();
$core = new Core();
$site = new Site();
$users = new Users();
$tpl = new Tpl();
$langs = new Lang();
$io = new Io();

// Get configuration
$config = $core->getConfig();

$captcha_private_key = $config[1]['captcha_private_key'];
$captcha_public_key = $config[1]['captcha_public_key'];

// Initialize system
$mysql->Initialize();
$site->Initialize();
$users->getSession();

$userdata = null;

function LaunchError($e) {
    global $tpl, $error;
    
    $tpl->setError($e);
    $error = true;
}

// mysql functions
function query($sql) {
    global $mysql;
    return $mysql->query($sql);
}

function query_rows($sql) {
    global $mysql;
    return $mysql->query_rows($sql);
}

function Contains($str, $words, $lower = false)
{
    if(!is_string($str))
        return $str;

    if($lower)
        $str = strtolower($str);

    if(!is_array($words))
        $wordss[] = $words;
    else
        $wordss = $words;

    foreach($wordss as $w)
    {
        if($lower)
            $w = strtolower($w);

        if(is_numeric(@strpos($str, $w)))
            return true;
    }

    return false;
}

function RequireLogin($r = true) {
    if($r) {
        
        if(LOGGED_IN) {
            Location('me');
        }
    }
    if(!$r) {
        if(!LOGGED_IN) {
            Location('index');
        }
    }
}

function ShortCuts($str, $other = '')
{
    $params = get_defined_constants(true);
    $params = $params['user'];

    if(is_array($other))
    {
        foreach($other as $param => $value)
                $params[$param] = $value;
    }

    foreach($params as $param => $value)
        $str = str_ireplace('{' . $param . '}', $value, $str);

    return $str;
}

function CleanVars() {
    global $core;
    $_POST = $core->FilterText($_POST, false);
    $_GET  = $core->FilterText($_GET, false);
}

function Location($url, $path = true, $https=false) {
    $go = ($path ? ($https ? HTTPS : WWW) . '/' . $url : $url);
    Header('Location: ' . $go);
    exit();
}

function is_captcha($challenge_field, $response) 
{
    global $captcha_private_key;
    
    require(NEC . "re-captchalib.php");
    $resp = recaptcha_check_answer ($captcha_private_key, USER_IP, $challenge_field, $response);
    return !$resp->is_valid ? false : true;
}

function getMonth($number) {
    $months = [
        "1" => "%month_1%",
        "2" => "%month_2%",
        "3" => "%month_3%",
        "4" => "%month_4%",
        "5" => "%month_5%",
        "6" => "%month_6%",
        "7" => "%month_7%",
        "8" => "%month_8%",
        "9" => "%month_9%",
        "10" => "%month_10%",
        "11" => "%month_11%",
        "12" => "%month_12%"
    ];
    
    return $months[$number];
}

function checkRegister($step)
{
    if(empty($_SESSION['quickregister']['step_timesmap']))
    {
        $_SESSION['quickregister']['step_timesmap'] = time();
    }
    else if(time() > $_SESSION['quickregister']['step_timesmap'] + 1200)
    {   
        unset($_SESSION['quickregister']);
        Location('quickregister/start', true, true);
    }
    
    if($step == 1)
    {
        if($_SESSION['quickregister']['step2']['complete'])
        {
            Location('quickregister/captcha', true, true);
        }
        else if($_SESSION['quickregister']['step1']['complete'])
        {
            Location('quickregister/email_password');
        }
    }
    else if($step == 2)
    {
        if(!$_SESSION['quickregister']['step1']['complete'])
        {
            Location('quickregister/age_gate');
        }
        else if($_SESSION['quickregister']['step2']['complete'])
        {
            Location('quickregister/captcha', true, true);
        }
    }
    else if($step == 3)
    {
        if(!$_SESSION['quickregister']['step1']['complete'])
        {
            Location('quickregister/age_gate');
        }
        else if(!$_SESSION['quickregister']['step2']['complete'])
        {
            Location('quickregister/email_password', true, true);
        }
    }
}
?>
